﻿using Exocortex.DSP;
using System;

namespace DinletBilsin
{
    public class HashGenerator
    {
        public HashGenerator() 
        {
            ChunkSize = Util.CHUNK_SIZE;
        }

        /// <summary>
        /// Aralıklardaki en yüksek magnitude sahip noktalar bulunur.
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        private int[] GetKeyPoints(Complex[] result)
        {
            int[] points = new int[] { 0, 0, 0, 0 };
            double[] score = new double[] { 0.0, 0.0, 0.0, 0.0 };
                      
            int index = 0;
            for (int i = Util.LOWER_FREQUENCY; i < Util.UPPER_FREQUENCY; i++)
            {              
                double mag = Math.Log(result[i].GetModulus() + 1);
              
                if (Util.FREQUENCY_INTERVAL[index] < i)
                    index++;
             
                if (mag > score[index]) {
                    score[index] = mag;
                    points[index] = i;
                }
            }
            return points;
        }
                
        private long Hash(int[] points) {
            return (points[3] & 0xFFFFFE) * (long)100000000
                + (points[2] & 0xFFFFFE) * (long)100000
                + (points[1] & 0xFFFFFE) * (long)100
                + (points[0] & 0xFFFFFE);
        }
        /// <summary>
        /// Hash yapar ve döner.
        /// Tüm data için dizi döner.
        /// </summary>
        /// <param name="complex"></param>
        /// <returns></returns>
        public long[] GetHashList(Complex[][] complex)
        {
            long[] hashes = new long[complex.Length];
            int index = 0;
            foreach (Complex[] line in complex)
            {
                hashes[index++] = Hash(GetKeyPoints(line));
            }

            return hashes;
        }
        public int ChunkSize { get; set; }
    }
}
